var paginationTotal = 20;//记录总数
var paginationPageSize  = 200;
var sortColumn = "";
var sortMethod = "";
var isIE = 0;
if (+[1,]){//非IE
	isIE = 0;
}else {//IE
	isIE = 1;
}

function isNum(s){
  //判断有效数字的正则表达式
   var pattern=/^[0-9]*[1-9][0-9]*$/;
	if(pattern.test(s))
	{
	   return true;
	}
	else
	{
		return false;        
	}        
 }

function CkAllPicking(o){
	$(o).is(":checked")?$("input[name='selected_picking_members[]']").attr("checked",true):$("input[name='selected_picking_members[]']").attr("checked",false);
}
$(function(){
	
	$.timepicker.regional['ru'] = {
		timeText: '<{$lang.pls}><{$lang.time}>',
		hourText: '<{$lang.hour}>',
		minuteText: '<{$lang.minite}>',
		secondText: '<{$lang.second}>',
		millisecText: '<{$lang.millisecond}>',
		currentText: '<{$lang.current}><{$lang.time}>',
		closeText: '<{$lang.ok}>',
		ampm: false
	};
	$.timepicker.setDefaults($.timepicker.regional['ru']);

	<{if $lang.current_language=='cn'}>	
	var dayNamesMin =['日','一', '二', '三', '四', '五', '六'];
    var monthNamesShort = ['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'];
    $('#dateFrom,#dateTo').datetimepicker({
        dayNamesMin:dayNamesMin,
        monthNamesShort:monthNamesShort,
        changeMonth:true,
        changeYear:true,
        hour: 15,
//        onClose: function(dateText, inst) {
//        	initData(0);
//        },
        dateFormat:'yy-mm-dd'       
    });
    <{else}>
    $('#dateFrom,#dateTo').datetimepicker({
        changeMonth:true,
        changeYear:true,
        hour: 15,
//        onClose: function(dateText, inst) {
//        	initData(0);
//        },
        dateFormat:'yy-mm-dd'       
    });
    <{/if}>
//	initData(0);
	$("#confirmDialog").dialog({
		autoOpen:false,
		modal:true,
		show:"slide",
		buttons:{
			"<{$lang.ok}>":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#ladingDialog").dialog({
		autoOpen:false,
		modal:true,
		show:"slide",
		
	});
	
	$(".search").click(function(){
		initData(0);
	});

    $("#confirmDialog2").dialog({
    		autoOpen:false,
    		modal:true,
    		show:"slide",
    		height:400,
    		width:400
    	});
    	$("#confirmDialog1").dialog({
    		autoOpen:false,
    		modal:true,
    		show:"slide",
    		width:600,
    		buttons:{
    			"<{$lang.ok}>":function(){
				  if($('input[name="selected_type"]:checked').val()==2){
				      if(!isNum($("#select_by_number").val())){
					    alert("<{$lang.pls_input_valid_numeric}>");
					    return;
					  }
				  }else{
				      if($(".selected_picking_members:checked").length==0){
    					alert("<{$lang.pls}><{$lang.pickup_user}>");
    					return;
    				 }
				  }
    				
    			
    				var members = "";
    				$(".selected_picking_members:checked").each(function(){
    					members+='<input type="hidden"   value="'+$(this).val()+'" name="picking_members[]">';
    				});
					
    				$("#pickup_user").html(members);
					var number_html="";
					number_html='<input type="hidden" name="by_number" id="by_number" value="'+$("#select_by_number").val()+'">';
					$("#pickup_number").html(number_html);
    				
					var selectType="";
					selectType_html='<input type="hidden" name="by_type" id="by_type" value="'+$('input[name="selected_type"]:checked').val()+'">';
					$("#pickup_type").html(selectType_html);
					
					$(this).dialog("close");
					
					$("#ladingDialog").html("<{$lang.system}><{$lang.processing}><{$lang.do_not}><{$lang.close}>").dialog("open");
                     
    				
					var vForm = {};
					vForm = $("#printForm").serialize();
					loading();
					$.ajax({
						type: "POST",
						async: false,
						dataType: "html",
						url: "/product/off-shelf-one-piece/print-orders/" ,
						data: vForm,
						success: function(html) {
							$("#print_html").val(html);
							$("#printFormHtml").submit();						
							closeLoading();
							$("#ladingDialog").dialog("close");
							setTimeout("initData(0)",1000);
 
						}
					});
					

    				$("#select_picking_members_clone").remove();
    			},
    			"<{$lang.close}>":function(){
    				$(this).dialog("close");
    			}
    		}
    	});



	$(".print").click(function(){
		if($(".checked:checked").length == 0){
			$("#confirmDialog").html("<{$lang.pls}><{$lang.order}>").dialog("open");
			return false;
		}

        var url='/product/off-shelf/getmember/';
	    var membersHtml='';
		var data={
				//opu_room_type:$("#warehouse_room_type").val(),
				status:1
		};
		loading();
		$.ajax({
			url:url,
			data:data,
			type:'get',
			dataType:'json',
			async:false,
			success:function(json){
				closeLoading();

				if(json.ask==1){
				   membersHtml+='<label style=\"width:85px;white-space:nowrap;\"><span style="display:inline-block;color:blue;"> <input  id="sel_member" style=\"float:left;padding-left: 100px;\" type="radio"  name="selected_type" value="1" checked class="selected_type"/>'+'<{$lang.pickup_user}>'+'</span></label>';
				    membersHtml+='<label style=\"width:85px;\"><span style="display:inline-block;color:blue;"> <input style=\"float:left;padding-left: 200px;\" type="radio"  name="selected_type" value="2" class="selected_type" id="sel_numer"/>'+'<{$lang.fengshu}>'+'</span></label>';
					
					membersHtml+='<label id="by_number_of" style=\"width:10px;padding-left:20px;display:none;\"><{$lang.input}><{$lang.fengshu}>:<span style="display:inline-block;"> <input type="text"  name="select_by_number" value="" size=5 id="select_by_number"/>'+'</span></label>';
					membersHtml+='<hr>';
					
					membersHtml+="<div style=\"float:left;padding-top:3px;\" id='member_div'>";
				   $.each(json.data,function(i,member){
						membersHtml+='<label style=\"float:left;width:85px;white-space:nowrap;\"><span style="display:inline-block;"> <input style=\"float:left;padding-left: 5px;\" type="checkbox" id="selected_picking_members_'+member.user_id+'" name="selected_picking_members[]" value="'+member.user_id+'_'+member.name+'" checked="true" class="selected_picking_members"/>'+member.name+'</span></label>';
				   });
				  
					membersHtml+='<span style="display:inline-block;color:blue;"><input type="checkbox" checked="true" onclick="CkAllPicking(this)" /><{$lang.select_all}></span>';			
					
				   membersHtml+="</div>";

				   membersHtml+="</div>";
					$("#confirmDialog1").html($("#select_picking_members").html(membersHtml)).dialog("open");
	               return false;

				}else{
				    $("#confirmDialog1").html("<{$lang.can_not_find}><{$lang.pickup_user}>");
					return false;
				}

			}
		});

	});
	$(".checkAll").click(function(){
		$(".checkAll").is(":checked")?$(".checked").attr("checked",true):$(".checked").attr("checked",false);
	});
	
	$(".sort").click(function(){
		sortColumn = $(this).attr("sortColumn");
		sortMethod = $(this).attr("sortMethod");
		$(".sort").children(".up").remove();
		if(sortMethod == 'asc'){
			$(this).attr("sortMethod","desc");
			$(this).append("<span class='up'>↑</span>");
		}else{
			$(this).attr("sortMethod","asc");
			$(this).append("<span class='up'>↓</span>");
		}
		
		initData(paginationCurrentPage-1);
	});

	//回车处理
	$("#customer_id").add("#orders_code").add("#<{$lang.sku}>").keyup(function(e){
		var key = e.which;
		if(key==13){
			initData(0);
		}
	});
	$(".selected_type").live("click",function(){
		if($(this).attr("id")=="sel_numer"){
		   $("#member_div").hide();
		   $("#by_number_of").show();
		   $("#select_by_number").focus();
		   
		}else{
		    $("#by_number_of").hide();
			$("#member_div").show();
		}
		
	});
});
function loadData(page, pageSize){
	
	$("#loadData").html('');
	var orderArea = [];
	$(".order_area:checked").each(function(){
		orderArea.push($(this).val());		
	});
	var params = {"orders_code":$("[name=orders_code]").val(),"<{$lang.sku}>":$("[name=<{$lang.sku}>]").val(),"category":$("[name=category]").val(),"orderPrintType":$("[name=orderPrintType]").val(),"customer_id":$("[name=customer_id]").val(),"ship_type":$("[name=ship_type]").val(),"sortColumn":sortColumn,"sortMethod":sortMethod,"order_area":orderArea };
	params.dateType = $("#dateType").val();
	params.dateFrom = $("#dateFrom").val();
	params.dateTo = $("#dateTo").val();
    $.ajax({
		url: "/product/off-shelf-one-piece/list/page/" + page + "/pageSize/" + pageSize,
        async: false,
        dataType: "json",
		type:"post",
		data:params,
        success: function(data) {
            loading();
			var html="";
			var json = data.data;
			paginationTotal = data.count;
			if(json.length>0){
				$(".checkAll").attr("checked",true);//<{$lang.default}><{$lang.all}>选中
				var j = page == 1 ? 1 : pageSize * (page - 1) + 1;
				$.each(json,function(k,v){
					html += '<tr><td><input type="checkbox" class="checked" checked name="orderCode[]" value="'+v.orders_code+'"/></td><td>'+(k+1)+'</td><td>'+v.orders_code+'</td><td>'+v.customer_id+'</td><td>'+data.status[v.process_status]+'</td><td>'+v.shipping_method+'</td><td>'+v.customer_order_code+'</td><td>'+v.add_time+'</td><td>'+v.ws_code+'</td><td>'+v.remark+'</td></tr>';
				});
			}else{
					html = '<tr><td colspan="10"><{$lang.no_data}></td></tr>';
			}
			
			
			$("#loadData").html(html);
			closeLoading();
        }
    });
}